
#include <stdio.h>
#include "command.h"

int tesCRC();
int tesParseReparse();
int tesBrokenCommand();
int tesOutOfBound();
int tesSpecialCase();
int main(int argc, char** argv) {
	printf("\n\n--------------\n");
    printf("commandTest\n");
    
    int i;
    
 
    i = tesCRC();	
	printf("tesCRC: %s\n",(i?"SUCCESS":"FAILED"));
    
    i = tesParseReparse();	
    printf("tesParseReparse: %s\n",(i?"SUCCESS":"FAILED"));
    
    i = tesBrokenCommand();    
    printf("tesBrokenCommand: %s\n",(i?"SUCCESS":"FAILED"));
    
    i = tesOutOfBound();    
    printf("tesOutOfBound: %s\n",(i?"SUCCESS":"FAILED"));
    
    i = tesSpecialCase();
    printf("tesSpecialCase: %s\n",(i?"SUCCESS":"FAILED"));
   
    printf("--------------------\n");
    printf("Contoh Command\n");
    
    command C;
    
    C = makeCommand(REPTEMP_CMD,  40, 50, 60);
    printCommand(C);
    printQue(makeQue(C));
    
    int x,y;
    for (x = 0; x < 25; x++){
        C = makeCommand(SETTEMP_CMD,  1, 2, x*10);
        printCommand(C);
        printQue(makeQue(C));
    }
    
    C = makeCommand(SETTEMP_CMD,  1, 2, 25);
    printCommand(C);
    printQue(makeQue(C));
    C = makeCommand(SETTEMP_CMD,  1, 2, 35);
    printCommand(C);
    printQue(makeQue(C));
    
    C = makeCommand(SETTEMP_CMD,  15, 13 , 50);
    printCommand(C);
    printQue(makeQue(C));
    
    
	C = makeCommand(SETPID_CMD,  51, 33, 20);
    printCommand(C);
    printQue(makeQue(C));
	
    C = makeCommand(SETID_CMD, 0, 0, 0);
    printCommand(C);
    printQue(makeQue(C));
    
    C = makeCommand(SETID_CMD,  1, 1, 0);
    printCommand(C);
    printQue(makeQue(C));
    
    C = makeCommand(SETID_CMD,  2, 2, 0);
    printCommand(C);
    printQue(makeQue(C));
    
    C = makeCommand(REPID_CMD,  4, 7, 0);
    printCommand(C);
    printQue(makeQue(C));
	

	return 0;
}


int tesCRC(){
    int success = 1;
	int crc;
	
    uint8_t data[9] = {49,50,51,52,53,54,55,56,57};
	crc = calculateCRC(data, 9);
	if (!isCRCEqual(crc,62)){success = 0;}
	
    return success;
}


int tesParseReparse(){
    int success = 1;
    int i;
    command c[8],cTemp;
	
    
	c[0] = makeCommand(SETTEMP_CMD,  2, 5, 35);
	c[1] = makeCommand(REPTEMP_CMD,  35, 27, 0);
	c[2] = makeCommand(SETPID_CMD,  51, 33, 20);
	c[3] = makeCommand(SETID_CMD,  5, 2, 0);
	c[4] = makeCommand(REPID_CMD,  22, 5, 0);
    for (i = 0 ; i < 5; ++i){
        cTemp = parseQue(makeQue(c[i]));
        if (!isCommandEqual(c[i], cTemp)){success = 0;}
    }
    
    return success;
}

int tesBrokenCommand(){
    int success = 1;
    int i;
    command c[5],cTemp2;

    
    cTemp2 = makeCommand(BROKEN_CMD,  2, 5, 35);;
	
	c[0] = makeCommand(SETTEMP_CMD,  2, 5, 35);
	c[1] = makeCommand(REPTEMP_CMD,  35, 27, 0);
	c[2] = makeCommand(SETPID_CMD,  51, 33, 20);
	c[3] = makeCommand(SETID_CMD,  5, 2, 0);
	c[4] = makeCommand(REPID_CMD,  22, 5, 0);
	
    for (i = 0 ; i < 5; ++i){
        if (isCommandEqual(c[i], cTemp2)){success = 0;}
    }
    
    return success;
}

int tesOutOfBound(){
    int success = 1;
    int i;
    command c[6], cTemp;
	c[0] = makeCommand(SETTEMP_CMD,  -1, 5, -3);
	c[1] = makeCommand(SETTEMP_CMD,  700, 500, UPPERBOUND);
	c[2] = makeCommand(SETTEMP_CMD,  -1, -9, -3);
	c[3] = makeCommand(SETTEMP_CMD,  UPPERBOUND, 0, UPPERBOUND);
	c[4] = makeCommand(SETTEMP_CMD,  0, UPPERBOUND, 0);
	c[5] = makeCommand(SETTEMP_CMD,  -UPPERBOUND, -UPPERBOUND, -UPPERBOUND);
    
    for (i = 0 ; i < 6; ++i){
        cTemp = parseQue(makeQue(c[i]));
        //printf("-- %d\n",i );
        //printCommand(c[i]);
        //printCommand(cTemp);
        if (!isCommandEqual(c[i], cTemp)){
            success = 0; 
        //    printf("ERR %d\n",i );
        }
    }
   
    return success;
}


int tesSpecialCase(){
    int success = 1;
    command c1,c2,c3;
	c1 = makeCommand(SETTEMP_CMD,  9, 9, 9);
    c2 = makeCommand(SETTEMP_CMD,  10, 10, 10);
    c3 = makeCommand(SETTEMP_CMD,  11, 11, 11);
    if (!isCommandEqual(c1, parseQue(makeQue(c1)))){success = 0;}
    if (!isCommandEqual(c2, parseQue(makeQue(c2)))){success = 0;}
    if (!isCommandEqual(c3, parseQue(makeQue(c3)))){success = 0;}
    if (isCommandEqual(c1, c2)){success = 0;}
    if (isCommandEqual(c2, c3)){success = 0;}
    if (isCommandEqual(c1, c3)){success = 0;}

    return success;
}

